/*
 * StateParser.java
 */
package strips;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayDeque;

/**
 *
 * @author mire
 */
public class StateParser {
    
    private StateParser() {
    }

    public static ArrayDeque<Predicate> parseStatesInLine(String inputLine) {

        ArrayDeque<Block> atributes;
        String input = inputLine;

        input = removeSpaces(input);
        input = input.replace(".", ";");
        String oldbracket = "),";
        String newbracket = ");";
        input = input.replace(oldbracket, newbracket);
        String[] strpred = input.split(";");

        ArrayDeque<Predicate> predicates = new ArrayDeque<Predicate>(strpred.length);
        for (int i = 0; i < strpred.length; i++) {
            Predicate p = parsePredicate(strpred[i]);
            if(!predicates.contains(p))
                predicates.add(p);
        }

        return predicates;
    }
    
    public static ArrayDeque<Block> parseAtributesInLine(String inputLine)
    {
        String input = inputLine;
        int index = 0;
        ArrayDeque<Block> atributes = new ArrayDeque<Block>();
        while( (index = input.indexOf('(', index+1)) != -1)
        {
            int indexEnd = input.indexOf(')', index+1);
            String attName = input.substring(index+1, indexEnd);
            if(attName.contains(","))
                continue;
            Block b = new Block(attName);
            if(!atributes.contains(b))
                atributes.add(b);
        }
            
        return atributes;
    }
    
    public static String[] parseFile(String fileName) {
        String[] initAndFinalStates = new String[2];

        try {
            FileInputStream fstream = new FileInputStream(fileName);
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            for (int i = 0; i < 2; i++) {
                if ((strLine = br.readLine()) == null) {
                    break;
                }
                initAndFinalStates[i] = strLine;

            }
            in.close();
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

        return initAndFinalStates;
    }

    private static String removeSpaces(String input) {
        String output = "";

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t') {
                output += c;
            }
        }

        return output;
    }

    private static Predicate parsePredicate(String input) {
        String name = null;
        ArrayDeque<Block> atributes = null;

        if (input.contains("(")) {
            name = input.substring(0, input.indexOf('('));
            atributes = new ArrayDeque<Block>(2);
            if (input.contains(",")) {
                String atr1 = input.substring(input.indexOf('(') + 1, input.indexOf(','));
                atributes.addFirst(new Block(atr1));
                String atr2 = input.substring(input.indexOf(',') + 1, input.indexOf(')'));
                atributes.addLast(new Block(atr2));
            } else {
                String atr = input.substring(input.indexOf('(') + 1, input.indexOf(')'));
                atributes.add(new Block(atr));
            }
        }

        if (atributes == null) {
            return new Predicate(input);
        }

        return new Predicate(name, atributes);

    }
}
